package com.myframework.servlets;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
 * Initialize the database configuration
 */
public class DbServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unused")
	private String configFilePath = "/WEB-INF/db-config.xml";

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		// Initialize the database configuration path
		String value = getServletConfig().getInitParameter("dbconfig");
		if (null != value) {
			this.configFilePath = value;
		}
		
		/* for now, we don't use db-config.xml file
		InputStream dbConfigInput;	// Input stream to our database configure file
		try {
			// Get an input stream to our database configure file
			dbConfigInput = getServletContext().getResourceAsStream(configFilePath);
			
			if (null == dbConfigInput) {
				WMLogger.error("Unable to retrieve resource at path: " + configFilePath);
				throw new Exception("Unable to retrieve resource at path: " + configFilePath);
			}
			WMLogger.info("Initialize the database configure file path: " + configFilePath);
			
			try {
				DBConfig.loadConfig(dbConfigInput, getServletConfig());
			} catch (Throwable t) {
				WMLogger.error("", t);
			}
		} catch (Throwable t) {
			WMLogger.error("", t);
		}
		*/
	}

	@Override
	public void destroy() {
		System.gc();
	}

}
